import Vue from 'vue'
/*@和..都可以,表示src文件*/
import HomeView from '../views/HomeView.vue'
import LayoutView from "@/views/LayoutView";
import MainView from "@/views/MainView";
import HomepageView from "@/views/HomepageView";
import RescueView from "@/views/RescueView";
/*防止报路径重复的错误*/
import VueRouter from 'vue-router'
import MyAuditView from "@/views/MyAuditView";
import MineView from "@/views/MineView";
import InfoView from "@/views/InfoView";
import ManagementView from "@/views/ManagementView";
import MyAppointmentView from "@/views/MyAppointmentView";
import ReceiveView from "@/views/ReceiveView";
import MyPutView from "@/views/MyPutView";
import MyGetView from "@/views/MyGetView";
import CompleteInfoView from "@/views/CompleteInfoView";
import EmpReserveView from "@/views/EmpReserveView";
import RemindView from "@/views/RemindView";
import AchievementView from "@/views/AchievementView";
import SeekView from "@/views/SeekView";

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}


Vue.use(VueRouter)

const routes = [
    {
        path: '/adminLogin',
        name: 'adminLogin',
        component: () => import('../views/LoginView.vue')
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('../views/RegisterView.vue')
    },
    {
        path: '/main',
        name: 'main',
        component: MainView,
        children: [
            {
                path: '/mine',
                name: 'mine',
                component: MineView,
                children: [
                    {
                        path: '/mine/myAudit',
                        name: 'myAudit',
                        component: MyAuditView
                    },
                    {
                        path: '/info',
                        name: 'info',
                        component: InfoView
                    },
                    {
                        path: '/management',
                        name: 'management',
                        component: ManagementView
                    },
                    {
                        path: '/MyAppointment',
                        name: 'MyAppointment',
                        component: MyAppointmentView
                    },
                    {
                        path: '/remind',
                        name: 'remind',
                        component: RemindView
                    },
                    {
                        path: '/receive',
                        name: 'receive',
                        component: ReceiveView
                    },
                    {
                        path: '/myPut',
                        name: 'myPut',
                        component: MyPutView
                    },
                    {
                        path: '/myGet',
                        name: 'myGet',
                        component: MyGetView
                    },


                ]
            },
            {
                path: '/homepage',
                name: 'homepage',
                component: HomepageView,
                meta: {role: 'ROLE_USER'},

                children: [
                    {
                        path: '',
                        name: 'dog',
                        component: () => import('../views/DogView.vue')
                    },
                    {
                        /*也可以写/cat*/
                        path: '/homepage/cat',
                        name: 'cat',
                        component: () => import('../views/CatView.vue'),
                        meta: {requiresAuth: true},
                    },
                    {
                        path: '/homepage/doctor',
                        name: 'doctor',
                        component: () => import('../views/DoctorView.vue'),
                        meta: {requiresAuth: true},
                    }
                ]
            },
            {
                path: '/rescue',
                name: 'home',
                component: RescueView,
                children: [
                    {
                        path: '/rescue/achievement',
                        name: 'achievement',
                        component: AchievementView,
                    },
                    {
                        path: '',
                        name: 'seek',
                        component: SeekView,
                    },
                ]
            },
        ]
    },
    {
        /*一级路由和二级路由的/时拼在一起的*/
        path: '/',
        name: 'Layout',
        component: LayoutView,
        meta: {requiresAuth: true, role: 'ROLE_ADMIN'},
        children: [

            {
                path: '',
                name: 'home',
                component: HomeView,

            },
            {
                path: 'animals',
                name: 'animals',
                component: () => import('../views/AnimalsView.vue')
            },
            {
                path: 'admin',
                name: 'admin',
                component: () => import('../views/AdminView.vue')
            },
            {
                path: 'employee',
                name: 'employee',
                component: () => import('../views/EmployeeView.vue')
            },
            {
                path: 'dept',
                name: 'dept',
                component: () => import('../views/DeptView.vue')
            },
            {
                path: 'audit',
                name: 'audit',
                component: () => import('../views/AuditView.vue')
            },
            {
                path: 'charts',
                name: 'charts',
                component: () => import('../views/ChartsView.vue')
            },
            {
                path: 'completeInfo',
                name: 'completeInfo',
                component: CompleteInfoView
            },
            {
                path: 'reserve',
                name: 'reserve',
                component: EmpReserveView
            },
            {
                path: 'user',
                name: 'user',
                component: () => import('../views/UserView.vue')
            }
        ]
    }

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
    const user = JSON.parse(localStorage.getItem("user"));
    const requiredRoles = to.meta.role; // 这里获取路由的role属性
    console.log(requiredRoles);
    // 路由需要登录权限
    console.log()
    if (to.matched.some(record => record.meta.requiresAuth)) {
        console.log('需要登录身份验证');
        if (user) {
            // 用户已登录
            const role = user.role;
            if (to.matched.some(record => record.meta.role !== role||role==='ROLE_DOCTOR')) {
                if (to.matched.some(record => record.meta.role === role||role==='ROLE_DOCTOR')) {
                    console.log('走这里');
                    next();
                } else {
                        next({name: 'homepage'});
                }
            } else {
                // 用户角色权限不足
                if (to.matched.some(record => record.meta.role !== role)) {
                        next({name: 'homepage'});
                } else {
                    next();
                }
            }
        } else {
            if (window.confirm('想了解更多?请先登录')) {
                return next('/adminLogin');
            }
            // 用户未登录

        }
    } else {
        console.log("不需要登录")
        // 路由不需要登录权限
        next();

    }
});

export default router
